package com.sky.robot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.robot.entity.SkyTableData;
import com.sky.robot.service.SkyTableDataService;
import com.sky.robot.utils.Result;
import com.sky.robot.utils.SkyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 需要支撑查询一个
 */
@RestController
@RequestMapping("/core/skyTableData")
public class SkyTableDataController {

    @Autowired
    private SkyTableDataService skyTableDataService;

    /**
     * 前端页面传过来第几页，每一页数据数量
     */
    @PostMapping("/listPage")
    public Object listPage(@RequestBody Map<String, Object> params) {
        Integer pageNum = Integer.parseInt((String) params.get("pageNum")); // 获取第几页
        Integer pageSize = Integer.parseInt((String) params.get("pageSize")); // 每页多少条数据
        IPage<SkyTableData> page = new SkyPage<>(pageNum, pageSize);
        Object obj = skyTableDataService.listPage(page, params);
        return Result.success(obj);
    }

    @PostMapping("/get")
    public Object get(@RequestBody Map<String, Object> params) {
        Object obj = skyTableDataService.get(params);
        return obj;
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody SkyTableData skyTableData) {
        Object obj = skyTableDataService.add(skyTableData);
        return Result.success(obj);
    }

    /**
     * 修改
     */
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody SkyTableData skyTableData) {
        Object obj = skyTableDataService.edit(skyTableData);
        return Result.success(obj);
    }

    /**
     * 通过id删除
     */

    @DeleteMapping("/delete")
    public Result<?> delete(String id) {
        skyTableDataService.delete(id);
        return Result.success(id);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        skyTableDataService.deleteBatch(ids);
        return Result.success(ids);
    }


    @GetMapping("/getTemporaryData")
    public Result<?> getTemporaryData(@RequestParam("tableName") String tableName) {
        Map<String, Object> list = skyTableDataService.getTemporaryData(tableName);
        return Result.success(list);
    }

    @PostMapping("/insertTemporaryData")
    public Result<?> insertTemporaryData(@RequestBody SkyTableData skyTableData) {
        Object obj = skyTableDataService.insertTemporaryData(skyTableData);
        return Result.success(obj);
    }

}
